package com.xmkj.bearbiz.home;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.Entity.HomeHeadEntity;
import com.common.banner.CBViewHolderCreator;
import com.common.banner.ConvenientBanner;
import com.common.banner.OnItemClickListener;
import com.common.cons.Constans;
import com.common.marquee.MarqueeFactory;
import com.common.marquee.MarqueeView;
import com.common.marquee.NoticeMF;
import com.common.mvp.BaseMvpFragment;
import com.common.mvp.BasePresenter;
import com.common.retrofit.method.GetHomeHeadsDatasMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.utils.SPUtils;
import com.xmkj.bearbiz.BaseUrlURLActivity;
import com.xmkj.bearbiz.R;
import com.xmkj.bearbiz.activity.MessageListActivity;
import com.xmkj.bearbiz.mine.activity.ChongZhiActivity;
import com.xmkj.bearbiz.my.MyShareOrderActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Create by leo
 * 创建日期：2018/5/3
 * 描述：
 */

public class HomeFragment extends BaseMvpFragment implements OnItemClickListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.home_view_pager)
    ConvenientBanner homeViewPager;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.iv_notice)
    ImageView ivNotice;
    @BindView(R.id.marqueeView)
    MarqueeView marqueeView;
    @BindView(R.id.swip_refresh)
    SwipeRefreshLayout swipRefresh;
    LinearLayout llSort;
    LinearLayout llShare;
    LinearLayout llChong;
    LinearLayout llNewHand;
    TabLayout tabLayout;
    ViewPager viewPager;
    SparseArray<String> titles = new SparseArray<>();
    private List<String> viewPagerUrl = new ArrayList<>();
    private HomeHeadEntity entity;

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void onViewCreated(View view) {
        homeViewPager = view.findViewById(R.id.home_view_pager);
        ivSearch = view.findViewById(R.id.iv_search);
        ivMessage = view.findViewById(R.id.iv_message);
        marqueeView = view.findViewById(R.id.marqueeView);
        swipRefresh = view.findViewById(R.id.swip_refresh);
        tabLayout = view.findViewById(R.id.tab_layout);
        viewPager = view.findViewById(R.id.view_pager);
        llSort = view.findViewById(R.id.ll_sort);
        llShare = view.findViewById(R.id.ll_share);
        llChong = view.findViewById(R.id.ll_chongzhi);
        llNewHand = view.findViewById(R.id.ll_newhand);
    }

    @Override
    protected void doLogicFunc() {
        swipRefresh.setOnRefreshListener(this);
        swipRefresh.setColorSchemeResources(R.color.basecolor);
        attachClickListener(ivMessage);
        attachClickListener(ivSearch);
        attachClickListener(llSort);
        attachClickListener(llShare);
        attachClickListener(llChong);
        attachClickListener(llNewHand);
        initTab();
        getHeadDatas();
    }

    private void getHeadDatas() {
        CommonSubscriber<HomeHeadEntity> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                swipRefresh.setRefreshing(false);
                dismissProgressDialog();
                entity = (HomeHeadEntity) o;
                if (entity.luck_info != null&&entity.is_luck==1) {
                    Intent intent = new Intent(context, ZhongJiangTipActivity.class);
                    intent.putExtra(ZhongJiangTipActivity.INFO, entity);
                    context.startActivity(intent);
                }
                setBanner();
                setMarQuee();
//                gotoHttpRep();
            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                swipRefresh.setRefreshing(false);
                showToastMsg(e);
            }
        });
        uid = SPUtils.getShareInt(Constans.UID);
        hashid = SPUtils.getShareString(Constans.HASHID);
        GetHomeHeadsDatasMethods.getInstance().appIndex(subscriber, uid, hashid);
        rxManager.add(subscriber);
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == ivMessage.getId()) {
            gotoActivity(MessageListActivity.class);
        } else if (view.getId() == ivSearch.getId()) {
            gotoActivity(SearchActivity.class);
        } else if (view.getId() == llSort.getId()) {
            gotoActivity(SortActivity.class);
        } else if (view.getId() == llShare.getId()) {
            gotoActivity(MyShareOrderActivity.class);
        } else if(view.getId()==llChong.getId()){
            gotoActivity(ChongZhiActivity.class);
        }else if(view.getId()==llNewHand.getId()){
            gotoActivity(NewHandActivity.class);
        }
    }

    private void initTab() {
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
        titles.append(0, "人气");
        titles.append(1, "最新");
        titles.append(2, "低");
        titles.append(3, "高");
        viewPager.setAdapter(new HomePagerAdapter(getChildFragmentManager(), titles, context));
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setMarQuee() {
        MarqueeFactory<TextView, String> marqueeFactory1 = new NoticeMF(context);
        List<String> datas = new ArrayList<>();
      /*  datas.add("恭喜您获得了100万奖励！");
        datas.add("恭喜您获得了与苍老师睡一晚的机会！");
        datas.add("恭喜您找到了一个漂亮的女朋友！");*/
        if (entity != null &&entity.open_list!=null&& entity.open_list.size() > 0) {
            for (int i = 0; i < entity.open_list.size(); i++) {
                datas.add(entity.open_list.get(i));
            }
        }
        marqueeFactory1.setData(datas);

        marqueeView.setMarqueeFactory(marqueeFactory1);
        marqueeView.startFlipping();
        marqueeFactory1.setOnItemClickListener(new MarqueeFactory.OnItemClickListener<TextView, String>() {
            @Override
            public void onItemClickListener(MarqueeFactory.ViewHolder<TextView, String> holder) {
//                RxBus.getDefault().post(new StartBrotherEvent(MessageFragment.newInstance("")));
//                showToastMsg("hhahahha");
            }
        });
    }

    private int[] page_indicatorId;

    private void setBanner() {
//        viewPagerUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525427891333&di=32991d497454624b5c490151a185a2c1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0142135541fe180000019ae9b8cf86.jpg%401280w_1l_2o_100sh.png");
//        viewPagerUrl.add("http://img07.tooopen.com/images/20170316/tooopen_sy_201956178977.jpg");
//        viewPagerUrl.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1525427891333&di=32991d497454624b5c490151a185a2c1&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0142135541fe180000019ae9b8cf86.jpg%401280w_1l_2o_100sh.png");
        if(viewPagerUrl.size()==0){
            if (entity != null && entity.banner_list.size() > 0) {
                for (int i = 0; i < entity.banner_list.size(); i++) {
                    viewPagerUrl.add(entity.banner_list.get(i).pic);
                }
            }
        }

        page_indicatorId = new int[]{R.drawable.ic_page_indicator, R.drawable.ic_page_indicator_focused};
        homeViewPager.setPageIndicator(page_indicatorId);
        homeViewPager.setPages(new CBViewHolderCreator<NetworkImageHolderView>() {
            @Override
            public NetworkImageHolderView createHolder() {
                return new NetworkImageHolderView();
            }
        }, viewPagerUrl);
        homeViewPager.setOnItemClickListener(this);
        homeViewPager.startTurning(5000);
    }


    @Override
    public void onItemClick(int position) {
        if (entity != null && entity.banner_list.size() > 0) {
            Intent intent1 = new Intent(context, BaseUrlURLActivity.class);
            intent1.putExtra(BaseUrlURLActivity.MAIN_URL, entity.banner_list.get(position).url);
            intent1.putExtra(BaseUrlURLActivity.TITILE, "活动");
            context.startActivity(intent1);
        }
    }


    @Override
    public void onRefresh() {
        getHeadDatas();
    }
}
